import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

//import org.activiti.engine.impl.util.json.JSONObject;

import org.json.JSONObject;

public class Project {

	private String address;
	private String description;
	private double price;
	private double expectedEarning;

	private HashMap<String, File> documents = new HashMap<String, File>();	// speichert Name und Dokument

	public Project(String address) {

		this.setAddress(address);
	}

	public Project(String address, String desctiption){

		this.setAddress(address);
		this.setDescription(desctiption);

	}

	public void addFile(String id, String link) {
		File file = new File(link);
		documents.put(id, file);
	}

	/*
	 * Dokument muss als String zurueckgegeben werden, da activiti nur einfache Datentypen (string, long, date, enum und boolean) unterstuetzt
	 */
	public String getFile(String id) {
		String content = readDocument(documents.get(id));

		return content;
	}

	private String readDocument(File requested) {
		String content = "";

		try {
			InputStreamReader reader = new InputStreamReader(new FileInputStream(requested));
			char[] fileContent = new char[(int)requested.length()];
			reader.read(fileContent);
			String fContentString = new String(fileContent);
			content += fContentString;

		} catch (FileNotFoundException e) {
			content = "File not found!";
		} catch (IOException e) {
			content = "Could not read file content!";
		}
		return content;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getExpectedEarning(){
		return expectedEarning;
	}

	public void setExpectedEarning(double ee){
		this.expectedEarning = ee;
	}

	public JSONObject toJSON() {
		try {
			JSONObject jsonobj = new JSONObject();
			jsonobj.put("address", this.address);
			jsonobj.put("description", this.description);
			jsonobj.put("price", this.price);
			return jsonobj;
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

